@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>修改生产领料单</title>
    <link href="~/scripts/layui-v2.5.6/layui/css/layui.css" rel="stylesheet" />
    <script src="~/scripts/jquery-3.4.1.min.js"></script>
    <script src="~/scripts/layui-v2.5.6/layui/layui.js"></script>
    <style>
        body {
            padding: 20px;
            background-color: #f8f9fa;
        }
        .form-container {
            background: #fff;
            padding: 20px;
            margin-bottom: 15px;
            border-radius: 4px;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
        }
        .material-container {
            background: #fff;
            padding: 20px;
            border-radius: 4px;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
        }
        .section-title {
            font-size: 16px;
            font-weight: bold;
            color: #333;
            margin-bottom: 15px;
            border-bottom: 2px solid #009688;
            padding-bottom: 5px;
        }
        .form-buttons {
            text-align: center;
            margin-top: 20px;
            padding-top: 15px;
            border-top: 1px solid #e6e6e6;
        }
        .layui-disabled {
            background-color: #f2f2f2 !important;
            cursor: not-allowed !important;
        }
    </style>
</head>
<body>
    <div class="layui-container-fluid">
        <!-- 基本信息表单 -->
        <div class="form-container">
            <div class="section-title">基本信息</div>
            <form class="layui-form" id="editPickingForm" lay-filter="editPickingForm">
                <input type="hidden" id="pickingId" name="id" value="@ViewBag.Id">
                <div class="layui-row layui-col-space15">
                    <div class="layui-col-md6">
                        <div class="layui-form-item">
                            <label class="layui-form-label"><span style="color: red;">*</span>领料单编号</label>
                            <div class="layui-input-block">
                                <input type="text" name="issuanceOrderCode" placeholder="请输入领料单编号" class="layui-input" lay-verify="required" readonly>
                            </div>
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <div class="layui-form-item">
                            <label class="layui-form-label"><span style="color: red;">*</span>领料单名称</label>
                            <div class="layui-input-block">
                                <input type="text" name="issuanceOrderName" placeholder="请输入领料单名称" class="layui-input" lay-verify="required">
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-row layui-col-space15">
                    <div class="layui-col-md6">
                        <div class="layui-form-item">
                            <label class="layui-form-label"><span style="color: red;">*</span>领料日期</label>
                            <div class="layui-input-block">
                                <input type="text" name="issuanceDate" placeholder="请选择日期" class="layui-input" id="issuanceDate" lay-verify="required">
                            </div>
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <div class="layui-form-item">
                            <label class="layui-form-label">领料仓库</label>
                            <div class="layui-input-block">
                                <select name="issuanceWarehouse" lay-search>
                                    <option value="">请选择</option>
                                    <option value="原料仓">原料仓</option>
                                    <option value="成品仓">成品仓</option>
                                    <option value="半成品仓">半成品仓</option>
                                    <option value="备件仓">备件仓</option>
                                </select>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-row layui-col-space15">
                    <div class="layui-col-md6">
                        <div class="layui-form-item">
                            <label class="layui-form-label">单据状态</label>
                            <div class="layui-input-block">
                                <select name="documentStatus">
                                    <option value="草稿">草稿</option>
                                    <option value="已确认">已确认</option>
                                    <option value="审批中">审批中</option>
                                    <option value="已审批">已审批</option>
                                    <option value="已完成">已完成</option>
                                    <option value="已拒绝">已拒绝</option>
                                    <option value="生成生产工具单">生成生产工具单</option>
                                </select>
                            </div>
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <div class="layui-form-item">
                            <label class="layui-form-label"><span style="color: red;">*</span>生产工单</label>
                            <div class="layui-input-block">
                                <input type="text" name="productionWorkOrder" placeholder="生产工单" class="layui-input" lay-verify="required" readonly>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-row layui-col-space15">
                    <div class="layui-col-md6">
                        <div class="layui-form-item">
                            <label class="layui-form-label">客户编号</label>
                            <div class="layui-input-block">
                                <input type="text" name="customerCode" placeholder="请输入客户编号" class="layui-input">
                            </div>
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <div class="layui-form-item">
                            <label class="layui-form-label">客户名称</label>
                            <div class="layui-input-block">
                                <input type="text" name="customerName" placeholder="请输入客户名称" class="layui-input">
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-row layui-col-space15">
                    <div class="layui-col-md6">
                        <div class="layui-form-item">
                            <label class="layui-form-label">生产任务</label>
                            <div class="layui-input-block">
                                <input type="text" name="productionTask" placeholder="请输入生产任务" class="layui-input">
                            </div>
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <div class="layui-form-item">
                            <label class="layui-form-label">任务名称</label>
                            <div class="layui-input-block">
                                <input type="text" name="taskName" placeholder="请输入任务名称" class="layui-input">
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-row layui-col-space15">
                    <div class="layui-col-md6">
                        <div class="layui-form-item">
                            <label class="layui-form-label">工作站</label>
                            <div class="layui-input-block">
                                <input type="text" name="workstation" placeholder="请输入工作站" class="layui-input">
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">备注</label>
                    <div class="layui-input-block">
                        <textarea name="remark" placeholder="请输入备注" class="layui-textarea" rows="3"></textarea>
                    </div>
                </div>
            </form>
        </div>

        <!-- 物料信息 -->
        <div class="material-container">
            <div class="section-title">物料信息</div>
            <!-- 工具栏 -->
            <div style="margin-bottom: 15px;">
                <button type="button" class="layui-btn layui-btn-sm" id="addMaterialBtn">
                    <i class="layui-icon layui-icon-add-1"></i> 新增
                </button>
                <button type="button" class="layui-btn layui-btn-sm layui-btn-normal" id="editMaterialBtn">
                    <i class="layui-icon layui-icon-edit"></i> 修改
                </button>
                <button type="button" class="layui-btn layui-btn-sm layui-btn-danger" id="deleteMaterialBtn">
                    <i class="layui-icon layui-icon-delete"></i> 删除
                </button>
            </div>
            <!-- 物料表格 -->
            <table class="layui-hide" id="materialTable" lay-filter="materialTable"></table>
        </div>

        <!-- 操作按钮 -->
        <div class="form-buttons">
            <button type="button" class="layui-btn" id="confirmBtn">
                <i class="layui-icon layui-icon-ok"></i> 确定
            </button>
            <button type="button" class="layui-btn layui-btn-primary" id="cancelBtn">
                <i class="layui-icon layui-icon-close"></i> 取消
            </button>
        </div>
    </div>

    <script>
        var materialData = []; // 存储物料数据

        layui.use(['form', 'table', 'laydate', 'layer'], function() {
            var form = layui.form;
            var table = layui.table;
            var laydate = layui.laydate;
            var layer = layui.layer;

            // 初始化日期选择器
            laydate.render({
                elem: '#issuanceDate',
                type: 'date'
            });

            // 初始化物料表格
            var materialTableIns = table.render({
                elem: '#materialTable',
                data: materialData,
                cols: [[ 
                    {type: 'checkbox', fixed: 'left'},
                    {field: 'productCode', title: '产品物料编号', width: 150},
                    {field: 'productName', title: '产品物料名称', width: 200},
                    {field: 'specifications', title: '规格型号', width: 120},
                    {field: 'unit', title: '单位', width: 80},
                    {field: 'quantity', title: '领料数量', width: 100, edit: 'text'},
                    {field: 'batch', title: '批次号', width: 120},
                    {field: 'warehouse', title: '仓库名称', width: 120},
                    {field: 'storageLocation', title: '库位名称', width: 120}
                ]],
                skin: 'line',
                even: true,
                page: false,
                height: 300
            });

            // 加载数据
            loadPickingData();

            // 确定按钮
            $('#confirmBtn').on('click', function() {
                // 验证表单
                var formData = form.val('editPickingForm');
                if (!formData.issuanceOrderCode) {
                    layer.msg('请输入领料单编号');
                    return false;
                }
                if (!formData.issuanceOrderName) {
                    layer.msg('请输入领料单名称');
                    return false;
                }
                if (!formData.issuanceDate) {
                    layer.msg('请选择领料日期');
                    return false;
                }
                if (!formData.productionWorkOrder) {
                    layer.msg('请选择生产工单');
                    return false;
                }

                // 显示加载层
                layer.load(2, {
                    shade: [0.3, '#000']
                });

                // 构造提交数据
                var submitData = {
                    id: $('#pickingId').val(),
                    issuanceOrderCode: formData.issuanceOrderCode,
                    issuanceOrderName: formData.issuanceOrderName,
                    issuanceDate: formData.issuanceDate,
                    issuanceWarehouse: formData.issuanceWarehouse || '',
                    productionWorkOrder: formData.productionWorkOrder,
                    documentStatus: formData.documentStatus || '草稿',
                    customerCode: formData.customerCode || '',
                    customerName: formData.customerName || '',
                    productionTask: formData.productionTask || '',
                    taskName: formData.taskName || '',
                    workstation: formData.workstation || '',
                    remark: formData.remark || '',
                    materials: materialData
                };

                // 模拟提交数据到服务器
                setTimeout(function() {
                    layer.closeAll('loading');
                    layer.msg('修改成功', {
                        icon: 1,
                        time: 2000
                    }, function() {
                        // 关闭弹窗并刷新父页面
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);
                        
                        // 刷新父页面的表格数据
                        if (parent.layui && parent.layui.table && parent.loadTableData) {
                            parent.loadTableData();
                        } else if (parent.window.loadTableData) {
                            parent.window.loadTableData();
                        } else {
                            // 如果找不到刷新函数，则刷新整个父页面
                            parent.location.reload();
                        }
                    });
                }, 1000);
            });

            // 取消按钮
            $('#cancelBtn').on('click', function() {
                var index = parent.layer.getFrameIndex(window.name);
                parent.layer.close(index);
            });

            // 加载领料单数据
            function loadPickingData() {
                var pickingId = $('#pickingId').val();
                if (!pickingId) {
                    layer.msg('无效的领料单ID');
                    return;
                }

                // 模拟加载数据
                setTimeout(function() {
                    // 模拟数据
                    $('input[name="issuanceOrderCode"]').val('ISSUE20250621002');
                    $('input[name="issuanceOrderName"]').val('1111-领料单');
                    $('input[name="issuanceDate"]').val('2025-06-21');
                    $('select[name="issuanceWarehouse"]').val('原料仓');
                    $('select[name="documentStatus"]').val('草稿');
                    $('input[name="productionWorkOrder"]').val('MO20250508002');
                    $('input[name="customerCode"]').val('C00008');
                    $('input[name="customerName"]').val('成聚');
                    $('input[name="productionTask"]').val('TASK20250048');
                    $('input[name="taskName"]').val('1111 【】 个');
                    $('input[name="workstation"]').val('测试');
                    
                    // 重新渲染表单
                    form.render();
                    
                    // 模拟物料数据
                    materialData = [
                        {
                            id: 1,
                            productCode: 'YCL00010',
                            productName: 'ET',
                            specifications: '20*20*20',
                            unit: 'm',
                            quantity: 1,
                            batch: '预置仓库',
                            warehouse: '预置库区',
                            storageLocation: '预置库位'
                        },
                        {
                            id: 2,
                            productCode: 'YCL00011',
                            productName: 'PP',
                            specifications: '50*50*50',
                            unit: 'm',
                            quantity: 2,
                            batch: '预置仓库',
                            warehouse: '预置库区',
                            storageLocation: '预置库位'
                        }
                    ];
                    
                    table.reload('materialTable', {
                        data: materialData
                    });
                }, 500);
            }
        });
    </script>
</body>
</html> 